﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using OperGui.ViewModel;

namespace OperGui.Views
{
    /// <summary>
    /// Interaction logic for ElementView.xaml
    /// </summary>
    public partial class ElementView : UserControl
    {
        Regex regex;
       


        public ElementView(string Id)
        {
            InitializeComponent();
            ItemsCollection();
        }

        private void ItemsCollection()
        {
            List<Elements> listElements = new List<Elements>();
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "50"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "10"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "30"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "60"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "7"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "100"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "100"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "100"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "100"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "100"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "100"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "100"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95")); 
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "100"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
            listElements.Add(new Elements("IFZ-MT001", "12:39:18", "Warning", "95"));
         
            DataList.ItemsSource = listElements;
            //DiagnosticData.ItemsSource = listElements;
        }
        
        class Elements
        {
            public string ErrorID { get; set; }
            public string Time { get; set; }
            public string EEvent { get; set; }
            public string Status { get; set; }
            public Elements(string _ErrorID, string _Time, string _EEvent, string _Status)
            {
                this.ErrorID = _ErrorID;
                this.Time = _Time;
                this.EEvent = _EEvent;
                this.Status = _Status;
            }
        }

        private void textboxsearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            {
                FindListViewItem(DataList);
            }
        }

        public void FindListViewItem(DependencyObject obj)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                ListViewItem lv = obj as ListViewItem;
                if (lv != null)
                {
                    HighlightText(lv);
                }
                FindListViewItem(VisualTreeHelper.GetChild(obj as DependencyObject, i));
            }
        }

        private void HighlightText(Object itx)
        {
            if (itx != null)
            {
                if (itx is TextBlock)
                {
                    regex = new Regex("(" + textboxsearch.Text + ")", RegexOptions.IgnoreCase);
                    TextBlock tb = itx as TextBlock;
                    if (textboxsearch.Text.Length == 0)
                    {
                        string str = tb.Text;
                        tb.Inlines.Clear();
                        tb.Inlines.Add(str);
                        return;
                    }
                    string[] substrings = regex.Split(tb.Text);
                    tb.Inlines.Clear();
                    foreach (var item in substrings)
                    {
                        if (regex.Match(item).Success)
                        {
                            Run runx = new Run(item);
                            runx.Background = Brushes.Red;
                            tb.Inlines.Add(runx);
                        }
                        else
                        {
                            tb.Inlines.Add(item);
                        }
                    }
                    return;
                }
                else
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
                    {
                        HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
                    }
                }
            }
        }

      

        private void DataList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Elements selectedrow = (Elements)DataList.SelectedItem;

            if (selectedrow != null)
            {

                (DataContext as MainWindowViewModel).performance = selectedrow.Status.ToString();

                //Do something with the id.
            }
        }
        

    }
}
